package com.lyj.web;

import com.lyj.bean.Customer;
import com.lyj.utils.JDBCUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

//tomcat来启动，还没写完
@WebServlet(name = "/ServletDemo02Client")
public class ServletDemo02Client extends HttpServlet {
    protected void doPost(HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException{
        //请求中文乱码
        request.setCharacterEncoding("utf-8");
        System.out.println("doPost:");

        try {
            Map<String, String[]> map = request.getParameterMap();
            Customer customer = new Customer();
            //customer数据放进map集合中
            BeanUtils.populate(customer , map);

            //保存到数据库用JDBCTemplate
            JdbcTemplate jdbcTemplate = new JdbcTemplate((DataSource) JDBCUtils.getConnection());
            int i = jdbcTemplate.update("insert into cst_customer values (?,?,?,?,?,?,?)",
                    customer.getCustId(),
                    customer.getCustName(),
                    customer.getCustSource(),
                    customer.getCustIndustry(),
                    customer.getCustLevel(),
                    customer.getCustPhone(),
                    customer.getCustMobile()
            );

            System.out.println(i);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException{

    }
}
